const db = require('../db/index')

// 获取用户列表的处理函数
exports.getUser = (req, res) => {
    const sql = `select * from user order by id asc`
    db.query(sql, (err, results) => {
        if (err) return res.cc(err)
        res.send({
            status: 0,
            message: '获取用户数据成功!',
            data: results
        })
    })
}

// 添加用户的处理函数
exports.addUser = (req, res) => {
    const sql = `insert into user set ?`
    db.query(sql, req.body, (err, results) => {
        // console.log(req.body)
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('新增用户失败')
        res.cc('新增用户成功!', 0)
    })

}

// 根据id删除用户的处理函数
exports.deleteUserById = (req, res) => {
    const sql = `delete from user where id=?`
    db.query(sql, req.params.id, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows === 1) {
            res.cc('删除用户成功!', 0)
        }
    })
}

// 根据id更新用户的处理函数
exports.updateUserById = (req, res) => {
    const sql = `update user set ? where id = ?`
    db.query(sql, [req.body, req.body.id], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('更新用户失败!')
        res.cc('更新用户成功!', 0)
    })
}

// 根据Name查找用户的处理函数(模糊查询)
exports.getUserByName = (req, res) => {
    const sql = `select * from user where name like ?`
    db.query(sql, req.body.name + '%', (err, results) => {
        let total = results.length
        // console.log(req.body.name)
        if (err) return res.cc(err)
        // if (results.length !== 1) return res.cc('获取用户失败!')
        res.send({
            status: 0,
            message: '获取用户成功!',
            data: {
                results,
                total
            }
        })
    })
}

// 根据id查找用户信息的处理函数
exports.getUserById = (req, res) => {
    const sql = `select * from user where id=?`
    db.query(sql, req.params.id, (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 1) return res.cc('获取用户失败!')
        res.send({
            status: 0,
            message: '获取用户成功!',
            data: results[0]
        })
    })
}

// 根据页面数据获取用户的处理函数
exports.getUserByPage = (req, res) => {
    let _offset = (req.params.page - 1) * req.params.pageSize
    const sql1 = `select * from user`
    db.query(sql1, (err, results) => {
        // 获取总人数
        let total = results.length
        let limit = +req.params.pageSize
        // 获取当前页人数
        const sql = `select * from user limit ?,?`
        db.query(sql, [_offset, limit], (err, results) => {
            if (err) return res.cc(err)
            res.send({
                status: 0,
                message: '获取用户成功!',
                data: {
                    results,
                    total
                }
            })
        })
    })

}

// 批量删除用户的处理函数
exports.batchDeleteUser = (req, res) => {
    const queryArray = Object.keys(req.body).map(key => +req.body[key])
    let placeHolders = queryArray.map(() => '?').join(',')
    const sql = `delete from user where id in(${placeHolders})`
    db.query(sql,queryArray, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows >= 1) {
            res.cc('删除用户成功!', 0)
        }
        else return res.cc('删除用户失败!可能是用户不存在!')
    })
}
